package com.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import android.os.Environment;
import android.util.Log;

public class FileUtils {

	private String SDPATH;

	public String getSDPATH() {
		return this.SDPATH;
	}

	public FileUtils() {
		this.SDPATH = Environment.getExternalStorageDirectory() + "/";
	}

	public String getFileAbsolutePath(String path, String filename) {
		String absolutePath = SDPATH + path + filename;
		Log.i("PDFVIEWER", "File absolute path : " + path);
		return absolutePath;
	}

	public static String getUpdateFile(String downloadFileName) {
		String[] dir = downloadFileName.split("_");
		String fileName = dir[0] + "_RapportFinal.pdf";
		return fileName;

	}

	public File createSDFile(String fileName) throws IOException {
		File file = new File(SDPATH, fileName);
		file.createNewFile();
		return file;

	}

	public File createSDDir(String dirName) {
		File dir = new File(SDPATH, dirName);
		dir.mkdir();
		return dir;
	}

	public boolean isFileExist(String fileName) {
		File file = new File(SDPATH, fileName);
		return file.exists();
	}

	public String getFileContent(String fileName) {
		String content = "";
		if (isFileExist(fileName)) {
			File file = new File(SDPATH, fileName);
			try {
				FileInputStream fstream = new FileInputStream(file);
				DataInputStream in = new DataInputStream(fstream);
				BufferedReader br = new BufferedReader(
						new InputStreamReader(in));
				String strLine;

				while ((strLine = br.readLine()) != null) {
					content = content + strLine + "\n";
				}
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		return content;
	}

	public File writeToSDFromString(String path, String fileName,
			String content) {

		File file = null;

		createSDDir(path);

		FileWriter fstream;
		try {
			file = createSDFile(path + fileName);
			fstream = new FileWriter(file);
			BufferedWriter out = new BufferedWriter(fstream);
			out.write(content);
			// Close the output stream
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return file;
	}

	public File writeToSDFromInput(String path, String fileName,
			InputStream input) {
		File file = null;
		OutputStream output = null;

		try {
			createSDDir(path);
			file = createSDFile(path + fileName);
			output = new FileOutputStream(file);

			byte buffer[] = new byte[1024];

			int len;
			while ((len = input.read(buffer)) > 0)
				output.write(buffer, 0, len);

			output.flush();
		}

		catch (Exception e) {
			e.printStackTrace();
		}

		finally {
			try {
				output.close();

			} catch (Exception e) {

				e.printStackTrace();
			}
		}
		return file;

	}

}
